package cn.com.tiza.spiro.jvm.gc;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by TZ0643 on 2015-12-30.
 *
 * Test GC cost time:
 *  Serial + Serial Old
 *  ParNew + Serial Old
 *  ParNew + CMS
 *  Parallel Scavenge + Parallel Old
 *
 * Options:
 *  -XX:+UseSerialGC
 *  -XX:+UseParNewGC
 *  -XX:+UseConcMarkSweepGC
 *  -XX:+UseParallelGC
 *
 */
public class GCTimeTest {
    static Map map = new HashMap();

    public static void main(String[] args) {
        long beginTime = System.currentTimeMillis();

        int iMax = Integer.valueOf(args[0]);

        iMax = Math.max(iMax, 1000);

        System.out.println("iMax = " + iMax);

        for(int i = 0; i < iMax; i++) {
            if(map.size() * 512 / 1024 / 1024 >= 400) {
                map.clear();
                System.out.println("map clean");
            }

            byte[] b1;
            for (int j = 0; j < 100; j++) {
                b1 = new byte[512];
                map.put(System.nanoTime(), b1);
            }
        }

        long endTime = System.currentTimeMillis();

        System.out.println(endTime - beginTime);
    }

}
